// https://github.com/react-native-device-info/react-native-device-info
import DeviceInfo from "react-native-device-info";

export type DeviceInfoType = {
  /** 系统名称 */
  systemName: string;
  /** 系统版本 */
  systemVersion: string;
  /** 品牌 */
  brand: string;
  /** api版本号 */
  APILevel: number;
  /** 设备名称 */
  deviceName: string;
  /** 用户 UA */
  userAgent: string;
  /** 首次安装时间 */
  firstInstallTime: number;
  /** 最近更新时间 */
  lastUpdateTime: number;
  /** mac 地址, android 10 有限制, 获取不到 */
  mac: string;
  /** IP 地址 */
  ip: string;
  /** Android ID */
  androidId: string;
  /** uniqueId */
  uniqueId: string;
  /** instanceId */
  instanceId: string;
  /** 电量 */
  batteryLevel: number;
  /** 唯一标识字符串 */
  fingerprint: string;
  /** 可用存储大小 */
  freeDiskStorage: number;
  /** 总存储大小 */
  totalDiskCapacity: number;
  /** 总内存大小 */
  totalMemory: number;
  /** 设备字体比例 */
  fontScale: number;

  /** 是否是平板电脑 */
  isTablet: boolean;
  /** 是否是横屏 */
  isLandscape: boolean;
  /** 是否连接键盘 */
  isKeyboardConnected: boolean;
  /** 是否连接鼠标 */
  isMouseConnected: boolean;
  /** 是否在充电 */
  isBatteryCharging: boolean;
};

/** 获取本地系统信息 */
export default (): Promise<DeviceInfoType> => {
  return new Promise(async (resolve, reject) => {
    try {
      const brand = DeviceInfo.getBrand();
      const APILevel = await DeviceInfo.getApiLevel();
      const deviceName = await DeviceInfo.getDeviceName();
      const systemName = DeviceInfo.getSystemName();
      const systemVersion = DeviceInfo.getSystemVersion();
      const mac = await DeviceInfo.getMacAddress();
      const ip = await DeviceInfo.getIpAddress();
      const uniqueId = await DeviceInfo.getUniqueId();
      const instanceId = await DeviceInfo.getInstanceId();
      const batteryLevel = await DeviceInfo.getBatteryLevel();
      const fingerprint = await DeviceInfo.getFingerprint();
      const freeDiskStorage = await DeviceInfo.getFreeDiskStorage();
      const totalDiskCapacity = await DeviceInfo.getTotalDiskCapacity();
      const totalMemory = await DeviceInfo.getTotalMemory();
      const fontScale = await DeviceInfo.getFontScale();

      const isTablet = DeviceInfo.isTablet();
      const isLandscape = await DeviceInfo.isLandscape();
      const isKeyboardConnected = await DeviceInfo.isKeyboardConnected();
      const isMouseConnected = await DeviceInfo.isMouseConnected();
      const isBatteryCharging = await DeviceInfo.isBatteryCharging();
      const userAgent = await DeviceInfo.getUserAgent();
      const firstInstallTime = await DeviceInfo.getFirstInstallTime();
      const lastUpdateTime = await DeviceInfo.getLastUpdateTime();

      // 这个 androidId 会出现多台设备重复的问题
      const androidId = await DeviceInfo.getAndroidId();

      resolve({
        brand,
        APILevel,
        deviceName,
        systemName,
        systemVersion,
        userAgent,
        firstInstallTime,
        lastUpdateTime,
        mac,
        ip,
        androidId,
        uniqueId,
        instanceId,
        batteryLevel,
        fingerprint,
        freeDiskStorage,
        totalDiskCapacity,
        totalMemory,
        fontScale,
        isTablet,
        isLandscape,
        isKeyboardConnected,
        isMouseConnected,
        isBatteryCharging
      });
    } catch (error) {
      console.log("error: ", error);
      reject({});
    }
  });
};
